package com.fxd.shopping.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.fxd.shopping.ware.param.PurchaseFinishParam;
import com.fxd.shopping.ware.param.PurchaseMergeParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fxd.model.ware.PurchaseEntity;
import com.fxd.shopping.ware.service.PurchaseService;
import com.fxd.core.util.PageUtils;
import com.fxd.core.util.RR;


/**
 * 采购信息
 *
 * @author fuxdong
 * @email 2016335176@qqcom
 * @date 2021-07-17 12:04:42
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public RR list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPage(params);

        return RR.ok().put("page", page);
    }

    /**
     * 采购完成
     */
    @RequestMapping("/done")
    public RR done(@RequestBody PurchaseFinishParam param) {
        purchaseService.finish(param);

        return RR.ok();
    }

    /**
     * 合并采购单
     */
    @PostMapping("/merge")
    public RR merge(@RequestBody PurchaseMergeParam param) {
        purchaseService.mergePurchase(param);

        return RR.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/unreceive/list")
    public RR unreceiveList(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPageUnreceive(params);

        return RR.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public RR info(@PathVariable("id") Long id) {
        PurchaseEntity purchase = purchaseService.getById(id);

        return RR.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public RR save(@RequestBody PurchaseEntity purchase) {
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());
        purchaseService.save(purchase);

        return RR.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public RR update(@RequestBody PurchaseEntity purchase) {
        purchaseService.updateById(purchase);

        return RR.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public RR delete(@RequestBody Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));

        return RR.ok();
    }

    /**
     * 领取采购单
     */
    @PostMapping("/received")
    public RR received(@RequestBody List<Long> ids) {
        purchaseService.received(ids);
        return RR.ok();
    }

}
